define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clothmaking_orders_product/index' + location.search,
                    add_url: 'clothmaking_orders_product/add',
                    edit_url: 'clothmaking_orders_product/edit',
                    del_url: 'clothmaking_orders_product/del',
                    multi_url: 'clothmaking_orders_product/multi',
                    import_url: 'clothmaking_orders_product/import',
                    table: 'clothmaking_orders_product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'product_id', title: __('Product_id'), formatter: function (value, row, index) {
                            if(row['product']!=null)
                                return '<a href="' + Fast.api.fixurl('clothmaking_product/index/index?id=' + value) + '" target="_blank">' + row['product']['name'] + '</a>';

                        }
                        },
                        {field: 'quantity', title: __('Quantity'), operate:'BETWEEN'},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'plan_id', title: __('Plan_id')},
                        {field: 'unitPrice', title: __('Unitprice'), operate:'BETWEEN'},
                        {field: 'totalPrice', title: __('Totalprice'), operate:'BETWEEN'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,

                            buttons: [
                                {
                                    name: 'addsub',
                                    title: '生产计划',
                                    classname: 'btn btn-warning btn-xs btn-dialog btn-addsub',
                                    icon: 'fa fa-plus',

                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('click', '.btn-addsub', function(e) {
                e.stopPropagation();

                var options = table.bootstrapTable('getOptions');

                var index = $(this).closest('tr').data('index');

                var data = table.bootstrapTable('getData');
             
                var id = data[index][options.pk]

                Fast.api.addtabs('clothmaking_plan/index?orderproduct_id='+ id , '订单产品生产计划列表')});
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
